import SwiftUI

import SwiftUI

//typealias Start<V> = Group<V> where V:View
//typealias End<V> = Group<V> where V:View

//struct ToggleView<Before: View,After:View>: View {
//    @State var flag:Bool = true
//    let before: Before
//    let after: After
//    var action1: ()->Void
//    var action2: ()->Void
//
//    init(@ViewBuilder before: @escaping () -> Before,@ViewBuilder after: @escaping ()->After,action1: @escaping () -> Void,action2: @escaping () -> Void) {
//        self.before = before()
//        self.after = after()
//        self.action1 = action1
//        self.action2 = action2
//    }
//
//    var body: some View {
//        if flag{
//            Button(action: {
//               self.flag.toggle()
//               action1()
//            }, label: {
//               before
//            })
//        }else{
//            Button(action: {
//                self.flag.toggle()
//                action2()
//            }, label: {
//                after
//            })
//        }
//    }
//}

//
//struct ToggleView<V1,V2>:View where V1:View,V2:View{
//    @State var flag:Bool = true
//
//    private let content: () -> TupleView<(Start<V1>,End<V2>)>
//
//    init(@ViewBuilder _ content: @escaping () -> TupleView<(Start<V1>,End<V2>)>) {
//            self.content = content
//    }
//
//    var body: some View{
//        let (start,end) = self.content().value
//        return HStack{
//            if flag {
//                Button(action: {
//                   self.flag.toggle()
//
//                }, label: {
//                   start
//                })
//            }else{
//                Button(action: {
//                    self.flag.toggle()
//                }, label: {
//                    end
//                })
//            }
//        }
//    }
//}
typealias Start<V> = Group<V> where V:View
typealias End<V> = Group<V> where V:View

struct ToggleView<V1, V2>: View where V1: View, V2: View {

    @State var flag:Bool = true
    private let content: () -> TupleView<(Start<V1>, End<V2>)>
    
    var action1: ()->Void
    var action2: ()->Void

    init(@ViewBuilder _ content: @escaping () -> TupleView<(Start<V1>, End<V2>)>,action1: @escaping () -> Void,action2: @escaping () -> Void) {
        self.content = content
        self.action1 = action1
        self.action2 = action2
    }

    var body: some View {
        let (start,end) = self.content().value
        return HStack {
            if(flag){
                Button(action: {
                    flag.toggle()
                    action1()
                }, label: {
                    start
                })
            }else{
                Button(action: {
                    flag.toggle()
                    action2()
                }, label: {
                    end
                })
            }
        }
    }
}

struct ThreeItemContainer_Previews: PreviewProvider {
    static var previews: some View {

        ToggleView({
            Start {
                Image(systemName: "envelope.fill")
            }
            End {
                Image(systemName: "chevron.right")
            }
        }, action1:{print("action1")},action2:{print("action2")})
}
}
